package ${basepackage}.framework.base.util;
import java.net.InetAddress;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




<#include "/author.include">
public class RandomGenerator
{
    /** The single instance of the Random object */
    private static SecureRandom randomGenerator;

    /** The host name of the local machine */
    private static String hostName;

    /** The basis for generating the random number */
    private static String seed;

    /** A Log instance for this class */
    private static Log log = LogFactory.getLog(RandomGenerator.class);

    static
    {
     
        try
        {
            randomGenerator = SecureRandom.getInstance("SHA1PRNG");
        }
         catch (NoSuchAlgorithmException nsae)
        {
            randomGenerator = new SecureRandom();
        }

      
        randomGenerator.nextBytes(new byte[1]);

       
        seed = new String(randomGenerator.generateSeed(12));
        seed += System.currentTimeMillis();

        try
        {
            hostName = InetAddress.getLocalHost().toString();
        }
         catch (Exception e)
        {
            hostName = new String(randomGenerator.generateSeed(8));
        }

        seed += hostName;

      
        randomGenerator.setSeed(seed.getBytes());
        log.info("Provider is:" + randomGenerator.getProvider());
    }

  
   
    public static synchronized void setSeed(long seed)
    {
        randomGenerator.setSeed(seed);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static synchronized Object[] generateRandomPositions(Object[] inputs)
    {
        HashMap unsortedMap = new HashMap();
        int inputSize = inputs.length;
		
        for (int i = 0; i < inputSize; i++)
        {
            int keyValue = randomGenerator.nextInt();
            unsortedMap.put(new Integer(keyValue), inputs[i]);
        }
        
      
        if (inputSize!=unsortedMap.size()) {
        	
        	return generateRandomPositions(inputs);        	
        }
		
        TreeMap sortedMap = new TreeMap(unsortedMap);
      

        return sortedMap.values().toArray();
    }

    public static synchronized boolean nextBoolean()
    {
        return randomGenerator.nextBoolean();
    }

    public static synchronized void nextBytes(byte[] bytes)
    {
        randomGenerator.nextBytes(bytes);
    }

    
    public static synchronized double nextDouble()
    {
        return randomGenerator.nextDouble();
    }

   
    public static synchronized float nextFloat()
    {
        return randomGenerator.nextFloat();
    }

    public static synchronized double nextGaussian()
    {
        return randomGenerator.nextGaussian();
    }


    public static synchronized String nextTranID(){
    	
    	try {
			Thread.sleep(10L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	
		
    	String tm = DateUtil.parseDate(Calendar.getInstance().getTime(), "yyyyMMddHHmmssSSS");
    	
    	String random = Integer.toString((new Random()).nextInt(1000));
    	
    	
    	String ID= tm+StringUtil.lPad(random, Integer.toString(999).length(),"0");
    	return ID;
    	
    }
    
    public static synchronized String nextID_32()
    {
    	int varChar =20;
    	
        long randomNo = randomGenerator.nextLong();
        String strRandomNo = Long.toHexString(randomNo);
        String strTimeStampVal = Long.toHexString(System.currentTimeMillis());
        char pad = '0';

        if (strRandomNo.length() < 16)
        {
            strRandomNo = StringUtil.rightAlignStr(strRandomNo, 16, pad);
        }

        if (strTimeStampVal.length() < 16)
        {
            strTimeStampVal = StringUtil.rightAlignStr(strTimeStampVal,
                    16, pad);
        }

        String temp = strRandomNo + strTimeStampVal;
        int len = temp.length();

        StringBuffer tempID = new StringBuffer();
        StringBuffer ID = new StringBuffer();

        for (int i = 1; i < len; i += 2)
        {
          
            tempID.append(String.valueOf(temp.charAt(i))).append(String.valueOf(
                    temp.charAt(i - 1)));
        }

       
        ID.append(tempID.substring(8, 16)).append(tempID.substring(16, 26))
          .append(tempID.substring(0, 8)).append(tempID.substring(26, 32));

        
        return ID.toString();
    }

    
    public static synchronized int nextInt()
    {
        return randomGenerator.nextInt();
    }

    
    public static synchronized int nextInt(int n)
    {
        return randomGenerator.nextInt(n);
    }

   
    public static synchronized long nextLong()
    {
        return randomGenerator.nextLong();
    }
}